local xiangshu = fk.CreateSkill {

  name = "joy__xiangshu",

  tags = { Skill.Limited, },

}



xiangshu:addEffect(fk.EventPhaseStart, {
  name = "joy__xiangshu",
  anim_type = "support",
  frequency = Skill.Limited,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(xiangshu.name) and player.phase == Player.Finish
    and player:usedSkillTimes(xiangshu.name, Player.HistoryGame) == 0
    and #player.room.logic:getActualDamageEvents( 1, function(e) return e.data[1].from == player end) > 0
    and table.find(player.room.alive_players, function(p) return p:isWounded() end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    player.room.logic:getActualDamageEvents( 1, function(e)
      if e.data[1].from == player then
        n = n + e.data[1].damage
      end
    end,Player.HistoryGame)
    local targets = table.map(table.filter(room.alive_players, function(p) return p:isWounded() end), Util.IdMapper)
    if #targets == 0 then return end
    n = math.min(n, 5)
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#xiangshu-invoke:::"..n, xiangshu.name, true)
    if #to > 0 then
      event:setCostData(self, {to[1], n})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self)[1])
    local n = event:getCostData(self)[2]
    room:recover({
      who = to,
      num = math.min(n, to:getLostHp()),
      recoverBy = player,
      skillName = xiangshu.name
    })
    if to.dead then return end
    to:drawCards(n, xiangshu.name)
  end,
})

return xiangshu